package com.atguigu.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;

/**
 * @author zhuguanming
 * @date 2021/5/23
 */
@Configuration
public class EsConfig {

    public static final RequestOptions COMMON_OPTIONS;

    @Autowired
    private Environment environment;

    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        COMMON_OPTIONS = builder.build();
    }

    @Bean
    public RestHighLevelClient esClient(){
        return new RestHighLevelClient(RestClient.builder(new HttpHost(environment.acceptsProfiles(Profiles.of("prod")) ? "gulimall-elasticsearch.gulimall" : "192.168.56.10", 9200, "http")));
    }
}
